from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session

from controller.AttendanceAdminController import AttendanceAdminController
from controller.PublicAdminController import PublicAdminController
from controller.StudentAdminController import StudentAdminController
from schema.QuerySchema import QueryBase, AttendanceQuery, AttendanceDateIntervalQuery
from schema.TeachingSchema import AttendanceInfo, BatchAttendanceInfo
from util.Auth import check_token
from util.DataBaseTools import get_db_session
from util.ErrorCode import ErrorCode
from util.LogHelper import Log

attendance_admin_router = APIRouter()


@attendance_admin_router.post("/attendance/mod", summary='根据考勤信息id修改考勤信息')
def mod_attendance(attendanceInfo: AttendanceInfo, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/attendance/mod')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/attendance/mod', db):
            response = AttendanceAdminController.mod_attendance(attendanceInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@attendance_admin_router.post("/attendance/queryall", summary='分页查询所有考勤信息')
def query_attendance_by_page(query_base: QueryBase, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/attendance/querybypage')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, "/attendance/query", db):
            response = AttendanceAdminController.query_attendance_by_page(query_base, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}

    else:
        return result


@attendance_admin_router.post("/attendance/query", summary='根据attend_date、student_id、is_normal、abnormal_type字段分页查询考勤信息')
def query_attendance_by_field(attendanceQuery: AttendanceQuery, db: Session = Depends(get_db_session),
                              result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/attendance/query')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/attendance/query', db):
            response = AttendanceAdminController.query_attendance_by_field(attendanceQuery, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@attendance_admin_router.post("/attendance/query/dateInterval", summary='根据考勤日期间隔、student_id,is_normal、abnormal_type字段分页查询考勤信息')
def query_attendance_by_dateInterval(attendanceDateIntervalQuery: AttendanceDateIntervalQuery, db: Session = Depends(get_db_session),
                              result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/attendance/query')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/attendance/query', db):
            response = AttendanceAdminController.query_attendance_by_dateInterval(attendanceDateIntervalQuery, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result

@attendance_admin_router.get("/attendance/query/{attend_date}/{class_id}", summary='根据attend_date、class_id字段查询学生考勤信息')
def query_attendance_by_class_and_date(attend_date: str, class_id: int, db: Session = Depends(get_db_session),
                              result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/attendance/query')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/attendance/query', db):
            response = AttendanceAdminController.query_attendance_by_class_and_date(attend_date, class_id, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@attendance_admin_router.post("/attendance/batchadd", summary='批量增加考勤信息')
def batch_add_attendance(batchAttendanceInfo: BatchAttendanceInfo, db: Session = Depends(get_db_session),
                         result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/attendance/batchadd')
    if result.get('code') == 0:
        port_id = result.get('data').port_id
        if PublicAdminController.has_permission(port_id, '/attendance/add', db):
            if port_id == 0 or port_id == 1 or port_id == 2 or port_id == 6:
                response = AttendanceAdminController.batch_add_attendance(batchAttendanceInfo, db)
                return response
            else:
                return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result
